﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace QUANLYDIEMHOCSINH
{
    public partial class chinhsuahs : Form
    {
        public chinhsuahs()
        {
            InitializeComponent();
            load_hs();  
        }
        int malop_cu;
        string hinhcu = "";
        private void load_hs()
        {
            int mahs = XemthongtinHS.maxoa;
            HOCSINHBUS hs = new HOCSINHBUS();
            HOCSINHDTO hocsinh= hs.lay_hocsinh(mahs);
            txthoten.Text = hocsinh.TENHS;
            txtsdt.Text = hocsinh.SODIENTHOAIPHUHUYNH;
            txtdiachi.Text = hocsinh.DIACHI;
            if (hocsinh.GIOITINH == 1)
                rdnam.Checked = true;
            else
                rdnu.Checked = true;
            DateTime dt = DateTime.Parse(string.Format("{0:dd/MM/yyyy}", hocsinh.NGAYSINH));
            dtngaysinh.Value = dt;
            if(hocsinh.ANH=="")
            {
                picanh.Image = Image.FromFile("hinh\\no_picture.PNG");
            }
            else
            {
                picanh.Image = Image.FromFile(hocsinh.ANH);
                hinhcu = hocsinh.ANH;
            }
            LOPBUS lopbus = new LOPBUS();
            cblop.DataSource = lopbus.layDSL();
            cblop.DisplayMember = "TENLOP";
            cblop.ValueMember = "MALOP";
            CHITIETHOCSINHBUS ct = new CHITIETHOCSINHBUS();
            cblop.SelectedValue = ct.lay_malop(mahs);
            malop_cu = ct.lay_malop(mahs);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HOCSINHBUS hs = new HOCSINHBUS();
            HOCSINHDTO hocsinh = new HOCSINHDTO();
            hocsinh.TENHS = txthoten.Text;
            DateTime dt = DateTime.Parse(dtngaysinh.Text, new CultureInfo("vi-VN", true));
            hocsinh.NGAYSINH = dt;
            hocsinh.DIACHI = txtdiachi.Text;
            hocsinh.SODIENTHOAIPHUHUYNH = txtsdt.Text;
            if (rdnam.Checked == true)
            {
                hocsinh.GIOITINH = 1;
            }
            else
                hocsinh.GIOITINH = 0;
            hocsinh.TINHTRANG = 1;
            if (ofdHinh.FileName != "")
            {
                picanh.Image.Save(Application.StartupPath + "\\hinh\\" + System.IO.Path.GetFileName(ofdHinh.FileName));
                hocsinh.ANH = "hinh\\" + System.IO.Path.GetFileName(ofdHinh.FileName);
            }
            else
            {
                hocsinh.ANH = hinhcu;
            }
            hocsinh.MAHS = XemthongtinHS.maxoa;
            int malop = int.Parse(cblop.SelectedValue.ToString());
            if (hs.chinhsua_hs(hocsinh,malop_cu,int.Parse(cblop.SelectedValue.ToString())) == 1)
                MessageBox.Show("Sửa Thành Công!");
            else
                MessageBox.Show("Sửa thất Bại!");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHinh_Click(object sender, EventArgs e)
        {
            ofdHinh.Filter = "file jpg(*.jpg)|*jpg";
            DialogResult dlgResual = ofdHinh.ShowDialog();
            if (dlgResual == DialogResult.OK)
            {
                picanh.ImageLocation = ofdHinh.FileName;
            }
        }
    }
}
